var verify = sessionStorage.getItem('verify');
var account = sessionStorage.getItem('account');
var character = sessionStorage.getItem('character');
var subCode = sessionStorage.getItem('subCode');
var subName = sessionStorage.getItem('subName');

//登录判断
switch (verify) {
    case 'true':
        var name = sessionStorage.getItem('name');
        document.getElementById("subName").innerText = subName;
        document.getElementById('subCode').innerText = subCode;

        var tName = sessionStorage.getItem('tName');
        if (character == 'student') { //学生登录才会传老师的名字到subInfo中
            document.getElementById('tName').innerText = "老师：" + tName;
        }
        // initFileInput("fileInput", "../submitHomework");
        break;

    case 'false':
        alert('请登录！');
        location.href = "login.html";
        break;
}

//fileInput设置
$("#fileInput").fileinput({
    language: 'zh', //设置语言
    uploadUrl: "/fileUpload", //上传的地址
    allowedFileExtensions: ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'],//接收的文件后缀
    browseClass: "btn btn-primary", //按钮样式

    uploadAsync: true, //默认异步上传
    showRemove: false, //显示移除按钮
    showPreview: false, //是否显示预览
    showCaption: false,//是否显示标题
    showUpload: false, //是否显示上传按钮
    showCancel: false,
    dropZoneEnabled: false,//是否显示拖拽区域
    maxFileCount: 1, //表示允许同时上传的最大文件个数
    enctype: '../submitHomework',
    validateInitialCount: true
});

//modal隐藏，清空输入框
$('#modalAddHome').on('hidden.bs.modal', function () {
    document.getElementById('modalAddHome_textHomeTitle').value = '';
    document.getElementById('modalAddHome_textHomeItem').value = '';
});
$('#modalSubmitHome').on('hidden.bs.modal', function () {
    document.getElementById('item').value = '';
});

//modal发布作业
$('#modalAddHome_btnSubmit').click(function () {
    var subCode = sessionStorage.getItem('subCode');
    var title = $('#modalAddHome_textHomeTitle').val();
    var item = $('#modalAddHome_textHomeItem').val();
    var deadLine = $('#modalAddHome_deadLine').val();

    if (title == '') {
        alert('必须有标题');
        return false;
    }

    if (deadLine == '') {
        alert('必须有截止时间');
        return false;
    }

    $.ajax({
        url: '/subject?method=addHomework',
        type: 'get',
        data: {subCode: subCode, title: title, item: item, deadLine: deadLine},
        dataType: 'json',
        success: function (result) {
            if (result) {
                alert("添加成功！");
                location.reload();
            }
            else {
                alert("添加成功！");
                location.reload();
            }

        }
    })
});

function HaveSubmit(haveSubmit, hId, account) {
    //判断是否已经提交作业

}

switch (character) {
    //学生div块动态加载
    case 'student':
        // $('#btn_submit').attr('class', 'show btn-primary');
        $.ajax({
            url: '/subject?method=findAllHomework',
            type: 'get',
            data: {subCode: subCode},
            dataType: 'json',
            success: function (result) {
                var i;
                for (i = 0; i < result.length; i++) {
                    var hId = result[i].hId;
                    var title = result[i].hTitle;
                    var item = result[i].hItem;
                    var deadLine = result[i].hDeadLine;
                    var haveSubmit;

                    // haveSubmit = HaveSubmit( hId, account);
                    $.ajax({
                        url: '/homework?method=findHaveSubmit',
                        type: 'get',
                        async: false,
                        data: {hId: hId, account: account},
                        dataType: 'json',
                        success: function (result) {
                            haveSubmit = result;
                        }
                    });

                    //若作业已提交
                    if (haveSubmit) {
                        var a = "<a id='btn_openModalSubmit' class='btn btn-primary btn-lg' style='float:right' " +
                            "disabled='disabled'" +
                            ">作业已提交" +
                            "</a>";
                        var b = "<a class='btn btn-warning btn-sm' style='float:right' " +
                            "onclick='disSubmit(this)' data-hId='"+ hId+"' data-account='"+ account+"'>撤销作业</a>";
                    }
                    //未提交作业时
                    else {
                        var a = "<a id='btn_openModalSubmit' class='btn btn-primary btn-lg' style='float:right' " +
                            "data-toggle='modal' data-target='#modalSubmitHome'" +
                            "data-hId='" + hId + "' data-account='" + account + "' " +
                            "onclick='setValue(this)'>上传作业" +
                            "</a>";
                        var b = "";
                    }


                    $('#div_subInfo').append("<div class='col-lg-12'>" +
                        "<div class='thumbnail'>" +
                        "<h1 class='text-left'>" + title +
                        a +
                        "</h1>" +
                        "<div class='caption'>" +
                        "<br>" +
                        "<h4>" + item + "" +
                        "</h4>" +
                        "<br>" +
                        "<h4>" +
                        "<p class='left'>截止日期:" + deadLine + "" +
                         b +
                        "</a>" +
                        "</p>" +
                        "</h4>" +
                        "</div>" +
                        "</div>" +
                        "</div>"
                    );
                }
            }
        });
        break;
    //老师div块动态加载
    case 'teacher':
        //显示发布作业按钮
        $('#btn_openModalAddHome').attr('class', ' btn btn-primary btn-lg');

        $.ajax({
            url: '/subject?method=findAllHomework',
            type: 'get',
            data: {subCode: subCode},
            dataType: 'json',
            success: function (result) {
                var i;
                for (i = 0; i < result.length; i++) {
                    var hId = result[i].hId;
                    var title = result[i].hTitle;
                    var item = result[i].hItem;
                    var deadLine = result[i].hDeadLine;


                    $('#div_subInfo').append("<div class='col-lg-12'>" +
                        "<div class='thumbnail'>" +
                        "<h1 class='text-left'>" + title +
                        "<a id='btn_checkHome' class='btn btn-primary btn-lg' style='float:right' onclick='checkHome(this)' " +
                        "data-hId = " + hId + " data-title='" + title + "' data-item='" + item + "' data-deadLine='" + deadLine + "'>批改" +
                        "</a>" +
                        "</h1>" +
                        "<div class='caption'>" +
                        "<br>" +
                        "<h4>" + item + "" +
                        "</h4>" +
                        "<br>" +
                        "<h4>" +
                        "<p class='left'>截止日期:" + deadLine + "" +
                        "<a id='btn_deleteHome' class='btn btn-warning' style='float:right' onclick='deleteHome(this)' " +
                        "data-hId = '" + hId + "' >删除作业" +
                        "</a>" +
                        "</p>" +
                        "</h4>" +
                        "</div>" +
                        "</div>" +
                        "</div>"
                    );
                }
            }
        });
        break;
}


//批改作业按钮事件
function checkHome(btn) {
    var hId = $(btn).data('hid');
    var title = $(btn).data('title');
    var item = $(btn).data('item');
    var deadLine = $(btn).data('deadline');

    sessionStorage.setItem('hId', hId);
    sessionStorage.setItem('title', title);
    sessionStorage.setItem('item', item);
    sessionStorage.setItem('deadLine', deadLine);

    location.href = 'homeworkInfo.html';
}

//上传作业按钮事件
function setValue(btn) {
    var hId = $(btn).data('hid');
    var account = sessionStorage.getItem('account');
    document.getElementById('homeworkId').value = hId;
    document.getElementById('stuAccount').value = account;
}


//删除作业事件
function deleteHome(btn) {
    var hId = $(btn).data('hid');

    if (confirm('是否确认删除？')) {
        $.ajax({
            url: '/subject?method=deleteHomework',
            type: 'get',
            data: {hId: hId},
            dataType: 'json',
            success: function (result) {
                if (result) {
                    location.reload();
                }
                else {
                    alert("删除失败");
                    location.reload();
                }
            }
        });
    }
    else {

    }
}

//撤销已经提交的作业
function disSubmit(btn) {
    var hId = $(btn).data('hid');

    if(confirm('是否撤销该作业？')){
        $.ajax({
           url:'/homework?method=disSubmit',
            type:'post',
            data:{hId:hId, account:account},
            datatype:'json',
            success:function (result) {
                if(result){
                    alert("撤销成功！");
                    location.reload();
                }
                else {
                    alert("撤销失败！");
                    location.reload();
                }
            }
        });
    }
}